package com.example.demo.controller;

import com.example.demo.entity.Notice;
import com.example.demo.service.NoticeService;
import com.example.demo.controller.vo.ResponseResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/admin/notice")
public class NoticeController {

    @Autowired
    private NoticeService noticeService;

    @GetMapping("/{page}")
    public ResponseResult<Map<String, Object>> getNoticeList(
            @PathVariable int page,
            @RequestParam(defaultValue = "10") Integer limit,
            @RequestParam(required = false) String keyword,  // 新增参数
            @RequestParam(required = false) String createTimeRange) {  // 新增参数

        log.info("Getting notice list - page: {}, size: {}, keyword: {}, createTimeRange: {}",
                page, limit, keyword, createTimeRange);

        Map<String, Object> data = noticeService.getNoticeList(page, limit, keyword, createTimeRange);
        log.info("Retrieved {} notices", data.get("totalCount"));
        return ResponseResult.success(data);
    }

    @PostMapping
    public ResponseResult<Notice> createNotice(
            @RequestParam String title,
            @RequestParam String content) {
        
        log.info("Creating new notice - title: {}, content: {}", title, content);
        
        Notice notice = noticeService.createNotice(title, content);
        log.info("Created notice with ID: {}", notice.getId());
        return ResponseResult.success(notice);
    }

    @PostMapping("/{id}")
    public ResponseResult<Boolean> updateNotice(
            @PathVariable Long id,
            @RequestParam String title,
            @RequestParam String content) {
        
        log.info("Updating notice - ID: {}, title: {}, content: {}", id, title, content);
        
        boolean success = noticeService.updateNotice(id, title, content);
        log.info("Notice update result - ID: {}, success: {}", id, success);
        return ResponseResult.success(success);
    }

    @PostMapping("/{id}/delete")
    public ResponseResult<Boolean> deleteNotice(
            @PathVariable Long id) {
        
        log.info("Deleting notice - ID: {}", id);
        
        boolean success = noticeService.deleteNotice(id);
        log.info("Notice deletion result - ID: {}, success: {}", id, success);
        return ResponseResult.success(success);
    }
}
